package com.wtb.filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wtb.pub.WtbException;
import com.wtb.utils.JdbcUtils;


public class TransationFilter implements Filter {
	private static final Log log = LogFactory.getLog(TransationFilter.class);
	public void destroy() {

	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletResponse res = (HttpServletResponse) response;
		try {
			chain.doFilter(request, response);
			JdbcUtils.commit();
		} catch (Exception e) {
			JdbcUtils.rollback();
			log.error("事务处理异常", e);
			res.sendRedirect("error?err_code=1004");
			throw new WtbException("事务处理异常",e);
		} finally {
			JdbcUtils.close();
		}

	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
